/* Copyright 2007-2008 dnAnalytics Project.
 *
 * Contributors to this file:
 * Patrick van der Velde
 * Marcus Cuda - http://marcus.cuda.net
 * Jurgen Van Gael
 *
 * This file is part of dnAnalytics.  dnAnalytics is licensed under the 
 * Microsoft Public License. See License.txt for a complete copy of the
 * license.
 */
using System;
using System.IO;

namespace dnAnalytics.LinearAlgebra.IO
{
    /// <summary>
    /// Writes an <see cref="Matrix"/> to delimited text file. If the user does not
    /// specify a delimiter, a tab separator is used.
    /// </summary>
    public class DelimitedMatrixWriter : MatrixWriter
    {

        private string delimiter;

        /// <summary>
        /// Constructs a <b>DelimitedMatrixWriter</b> using a comma as the delimiter.
        /// </summary>
        public DelimitedMatrixWriter()
        {
            delimiter = "\t";
        }

        /// <summary>
        /// Constructs a <b>DelimitedMatrixWriter</b> using the given delimiter.
        /// </summary>
        /// <param name="delimiter">the delimiter to use.</param>
        public DelimitedMatrixWriter(char delimiter)
        {
            this.delimiter = new string(delimiter, 1);
        }

        /// <summary>
        /// Constructs a <b>DelimitedMatrixWriter</b> using the given delimiter.
        /// </summary>
        /// <param name="delimiter">the delimiter to use.</param>
        public DelimitedMatrixWriter(string delimiter)
        {
            this.delimiter = delimiter;
        }

        /// <summary>
        /// Writes the given <see cref="Matrix"/> to the given <b>TextWriter</b>.
        /// </summary>
        /// <param name="matrix">The matrix to write.</param>
        /// <param name="writer">The <see cref="TextWriter"/> to write the matrix to.</param>
        /// <param name="format">The format to use on each element.</param>
        /// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="writer"/> is null.</exception>
        protected override void DoWriteMatrix(Matrix matrix, TextWriter writer, string format)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            int cols = matrix.Columns - 1;
            int rows = matrix.Rows - 1;
            for (int i = 0; i < matrix.Rows; i++)
            {
                for (int j = 0; j < matrix.Columns; j++)
                {
                    writer.Write(matrix[i, j].ToString(format, System.Globalization.CultureInfo.InvariantCulture));
                    if (j != cols)
                    {
                        writer.Write(delimiter);
                    }
                }
                if (i != rows)
                {
                    writer.Write(Environment.NewLine);
                }
            }
        }
    }
}
